#include <cstdio>

const int N = 1e5 + 5;
int n, tot, rt;

struct Node {
  int ch[2], fa, val, cnt, size;
} tree[N];

inline bool iden(int x) { return tree[tree[x].fa].ch[1] == x; }

inline void update(int x) {
  if (x) {
    tree[x].size = tree[x].cnt;
    if (tree[x].ch[0]) tree[x].size += tree[tree[x].ch[0]].size;
    if (tree[x].ch[1]) tree[x].size += tree[tree[x].ch[1]].size;
  }
}

inline void connect(int x, int y, bool op) {
  if (x) tree[x].fa = y;
  if (y) tree[y].ch[op] = x;
}

inline void rotate(int x) {
  int fa = tree[x].fa, ffa = tree[fa].fa;
  bool n = iden(fa), m = iden(x);
  connect(tree[x].ch[m ^ 1], fa, m);
  connect(fa, x, m ^ 1);
  connect(x, ffa, n);
  update(fa);
  update(x);
}

inline void splay(int x) {
  for (int fa; (fa = tree[x].fa); rotate(x))
    if (tree[fa].fa) rotate(iden(fa) == iden(x) ? fa : x);
  rt = x;
}

inline void insert(int x) {
  if (!rt) {
    rt = ++tot;
    tree[rt].val = x;
    tree[rt].size = tree[rt].cnt = 1;
    tree[rt].ch[0] = tree[rt].ch[1] = 0;
    return;
  }
  int cur = rt, fa = 0;
  while (true) {
    if (tree[cur].val == x) {
      tree[cur].cnt++;
      update(cur);
      update(fa);
      splay(cur);
      break;
    }
    fa = cur, cur = tree[cur].ch[x > tree[cur].val];
    if (!cur) {
      tree[++tot].val = x;
      tree[tot].cnt = 1;
      tree[tot].ch[0] = tree[tot].ch[1] = 0;
      tree[tot].size = 1;
      tree[tot].fa = fa;
      tree[fa].ch[x > tree[fa].val] = tot;
      update(fa);
      splay(tot);
      break;
    }
  }
}

inline int find(int x) {
  int res = 0, cur = rt;
  while (cur) {
    if (x < tree[cur].val) {
      cur = tree[cur].ch[0];
    } else {
      res += tree[tree[cur].ch[0]].size;
      if (x == tree[cur].val) {
        splay(cur);
        return res + 1;
      }
      res += tree[cur].cnt;
      cur = tree[cur].ch[1];
    }
  }
  if (cur) splay(cur);
  return res;
}

inline int rank(int x) {
  int cur = rt;
  while (cur) {
    if (x <= tree[tree[cur].ch[0]].size) {
      cur = tree[cur].ch[0];
      continue;
    }
    if (tree[tree[cur].ch[0]].size < x &&
        x <= tree[tree[cur].ch[0]].size + tree[cur].cnt)
      break;
    else {
      x -= tree[tree[cur].ch[0]].size + tree[cur].cnt;
      cur = tree[cur].ch[1];
    }
  }
  if (cur) splay(cur);
  return tree[cur].val;
}

inline int upper(int x) {
  int cur = rt, res = 0x7fffffff;
  while (cur) {
    if (tree[cur].val < res && x < tree[cur].val) res = tree[cur].val;
    if (tree[cur].val > x) cur = tree[cur].ch[0];
    else
      cur = tree[cur].ch[1];
  }
  return res;
}

inline int lower(int x) {
  int cur = rt, res = -0x7fffffff;
  while (cur) {
    if (tree[cur].val > res && tree[cur].val < x) res = tree[cur].val;
    if (tree[cur].val < x) cur = tree[cur].ch[1];
    else
      cur = tree[cur].ch[0];
  }
  return res;
}

inline void clear(int x) {
  tree[x].ch[0] = tree[x].ch[1] = tree[x].cnt = tree[x].fa = tree[x].val =
      tree[x].size = 0;
}

inline void del(int x) {
  find(x);
  if (tree[rt].cnt > 1) {
    --tree[rt].cnt;
    update(rt);
    return;
  }
  if (!tree[rt].ch[0] && !tree[rt].ch[1]) {
    clear(rt);
    rt = 0;
    return;
  }
  if (!tree[rt].ch[0]) {
    int org = rt;
    rt = tree[rt].ch[1];
    tree[rt].fa = 0;
    clear(org);
    return;
  }
  if (!tree[rt].ch[1]) {
    int org = rt;
    rt = tree[rt].ch[0];
    tree[rt].fa = 0;
    clear(org);
    return;
  } else {
    int lef = tree[rt].ch[0], org = rt;
    while (tree[lef].ch[1]) lef = tree[lef].ch[1];
    splay(lef);
    connect(tree[org].ch[1], rt, 1);
    clear(org);
    update(rt);
  }
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen64("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen64("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("Splay.in", "r", stdin);
  freopen("Splay.out", "w", stdout);
#endif
#endif

  scanf("%d", &n);
  int opt, x;
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &opt, &x);
    switch (opt) {
      case 1:
        insert(x);
#ifdef DEBUG
        printf("case%d: %d ", opt, i);
#endif
        break;
      case 2:
        del(x);
#ifdef DEBUG
        printf("case%d: %d ", opt, i);
#endif
        break;
      case 3:
        printf("%d\n", find(x));
#ifdef DEBUG
        printf("case%d: %d ", opt, i);
#endif
        break;
      case 4:
        printf("%d\n", rank(x));
#ifdef DEBUG
        printf("case%d: %d ", opt, i);
#endif
        break;
      case 5:
        printf("%d\n", lower(x));
#ifdef DEBUG
        printf("case%d: %d ", opt, i);
#endif
        break;
      case 6:
        printf("%d\n", upper(x));
#ifdef DEBUG
        printf("case%d: %d ", opt, i);
#endif
        break;
    }
#ifdef DEBUG
    puts("");
    debug(rt);
    puts("");
    fflush(stdout);
#endif
  }
  return 0;
}